﻿/*
 * Copyright 2014 Francois Karman
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.ComponentModel.DataAnnotations;

namespace Quartz.WebManager.Model
{
    /// <summary>
    /// Tests the <see cref="LowerOrEqualToAttribute"/> class.
    /// </summary>
    [TestClass]
    public class LowerOrEqualToAttributeTest
    {
        /// <summary>
        /// Tests the <see cref="LowerOrEqualToAttribute"/> constructor.
        /// </summary>
        [TestMethod]
        public void Constructor()
        {
            LowerOrEqualToAttribute instance = new LowerOrEqualToAttribute("test linked");
            Assert.AreEqual("test linked", instance.LinkedProperty);
        }
        /// <summary>
        /// Tests the <see cref="LowerOrEqualToAttribute.RequiresValidationContext"/> property.
        /// </summary>
        [TestMethod]
        public void RequiresValidationContext()
        {
            ValidationAttribute instance = new LowerOrEqualToAttribute("test linked");
            Assert.IsTrue(instance.RequiresValidationContext);
        }

        /// <summary>
        /// Tests the <see cref="LowerOrEqualToAttribute.FormatErrorMessage"/> method.
        /// </summary>
        [TestMethod]
        public void FormatErrorMessage()
        {
            ValidationAttribute instance = new LowerOrEqualToAttribute("test linked");
            string actual = instance.FormatErrorMessage("test name");
            Assert.IsNotNull(actual);
            StringAssert.Contains(actual, "test name");
        }

        /// <summary>
        /// Tests the <see cref="LowerOrEqualToAttribute.IsValid"/> method.
        /// </summary>
        [TestMethod]
        public void IsValid()
        {
            ValidationContext context = new ValidationContext(new TestInstance() { IntValue = 10, StringValue = "string" });
            LowerOrEqualToAttribute instance = new LowerOrEqualToAttribute("IntValue");

            Assert.IsNull(instance.GetValidationResult(null, context));

            ValidationResult actual;
            Assert.IsNull(instance.GetValidationResult(5, context));
            Assert.IsNull(instance.GetValidationResult(10, context));
            actual = instance.GetValidationResult(20, context);
            Assert.IsNotNull(actual);
        }

        private class TestInstance
        {
            public int IntValue { get; set; }
            public string StringValue { get; set; }
        }
    }
}
